#include "workerautosave.h"

#include "livetrack.h"
#include "livetrackautosaver.h"

WorkerAutoSave::WorkerAutoSave(QFile* f, LiveTrack* t ) :
    WorkerProcess(2, tr("Preparing LiveTrack..."))
{
    file = f;
    livetrack = t;
}

bool WorkerAutoSave::do_stuff()
{
    if ( !file->isOpen() )
    {
        setErrorString( tr("File is not open"));
        return false;
    }

    GPS_track* track = livetrack->_track;
    int n;
    QTextStream stream( file );

    QList<int> listLegs = track->listLegs();
    QList<int> waypointIds = track->listWaypoints();
    int totWaypoints = waypointIds.count();
    int totPoints = 0;
    foreach (int leg_id, listLegs )
    {
        const GPS_leg* leg = track->leg(leg_id);
        if (  leg != NULL )
            totPoints += leg->nPoints();
    }
    setMinMaxProgress( totWaypoints + totPoints );

    int nElements = 0;
    QString waypoints_str = tr("Saving waypoints: %1 / %2");
    QString points_str = tr("Saving points: %1 / %2");

    updateText(0, waypoints_str.arg(0).arg(totWaypoints) );
    updateText(1, points_str.arg(0).arg(totPoints) );
    updateProgress( nElements );

    // Save general data...
    stream << LiveTrackAutosaver::formatTitle( track->title () );
    stream << LiveTrackAutosaver::formatDescription( track->description() );
    stream << LiveTrackAutosaver::formatFilename( livetrack->_filename );
    stream << LiveTrackAutosaver::formatActiveLeg( livetrack->_activeLeg );

    // Save waypoints...
    n = 0;
    foreach (int id, waypointIds )
    {
        const GPS_waypoint* wp = track->waypoint( id );
        if ( wp != NULL )
            stream << LiveTrackAutosaver::formatWaypoint( wp );
        n++;
        nElements++;
        updateText(0, waypoints_str.arg(n).arg(totWaypoints) );
        updateProgress( nElements );
        if ( please_terminate )
            return false;
    }
    updateText(0, tr("Saved waypoints: %1").arg(totWaypoints) );
    if ( please_terminate )
        return false;

    // Save Legs...
    n = 0;
    foreach (int leg_id, listLegs )
    {
        const GPS_leg* leg = track->leg(leg_id);
        if (  leg != NULL )
        {
            stream << LiveTrackAutosaver::formatLeg( leg );
            for ( int i = 0; i < leg->nPoints(); i++ )
            {
                const GPS_point* pt = leg->point(i);
                if ( pt != NULL )
                {
                    stream << LiveTrackAutosaver::formatPoint( leg_id, pt );
                    n++;
                    nElements++;
                    updateText(1, points_str.arg(n).arg(totPoints) );
                    updateProgress( nElements );
                    if ( please_terminate )
                        return false;
                }
            }
        }
        if ( please_terminate )
            return false;
    }
    updateText(0, tr("Saved points: %1").arg(totPoints) );
    updateProgress( nElements );
    if ( please_terminate )
        return false;

    return true;
}
